package middleware

import (
	"fmt"
	"vblog/apps/token"
	"vblog/apps/user"
	"vblog/response"

	"github.com/gin-gonic/gin"
)

// 角色参数个数不确定吗 ？
// token中的role不保存数据库，校验token时到user对象里查询
func RequireRole(requiredRoles ...user.Role) func(ctx *gin.Context) {
	return func(ctx *gin.Context) {
		fmt.Println(ctx.Get(token.GinCookieName))
		if v, ok := ctx.Get(token.GinCookieName); ok {
			for i := range requiredRoles {
				requiredRole := requiredRoles[i]
				if v.(*token.Token).Role == requiredRole {
					fmt.Println(requiredRole)
					fmt.Println(v)
					ctx.Next()
					return

				}
			}
		}
		response.Failed(ctx, ErrAPermissionDeny)
		ctx.Abort()
	}
}
